package one.jieyun.ajzw;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2016/9/12.
 */
public class Activity_View  extends BaseActivity{

    private String mTitle;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);

        final String mUrl;
        Intent intent = this.getIntent();     //获取已有的intent对象
        Bundle bundle = intent.getExtras();    //获取intent里面的bundle对象
        mUrl = bundle.getString("url");
        mTitle = bundle.getString("title");

        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                onCreateDelay(mUrl);
                return false;
            }
        });

        initStatus();

        setStatusBar();
    }

    protected void onCreateDelay(String mUrl) {
        WebView view = (WebView)findViewById(R.id.webView);
        view.loadUrl(mUrl);

        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        view.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
    }

    private void initStatus(){
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(mTitle);

        LinearLayout linearLayout = (LinearLayout)findViewById(R.id.topPanel);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)linearLayout.getLayoutParams();
        layoutParams.topMargin = getTitleHeight();
        linearLayout.setLayoutParams(layoutParams);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case(android.R.id.home):
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


}
